/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.TblRawMt;
import java.util.Date;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class TblRawMtFacade extends AbstractFacade<TblRawMt> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TblRawMtFacade() {
        super(TblRawMt.class);
    }

    public List<String> findAllShortCode() throws Exception {
        try {
            TypedQuery<String> q = em.createNamedQuery("TblRawMt.findAllShortCode", String.class);
            return q.getResultList();
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(TblRawMtFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<TblRawMt> find(Date startDate, Date endDate, List<String> shortCodes, List<String> provinces, List<String> operators) throws Exception {
        try {
            StringBuilder query = new StringBuilder();
            query.append("SELECT t FROM TblRawMt t WHERE t.sc in :shortCodes");
            query.append(" AND t.time BETWEEN :startDate and :endDate");
            if (operators != null && !operators.isEmpty()) {
                query.append(" AND t.telco IN :operators");
            }
            if (provinces != null && !provinces.isEmpty()) {
                query.append(" AND t.gen2 IN :provinces");
            }
            query.append(" ORDER BY t.sc, t.time DESC, t.content");
            TypedQuery<TblRawMt> q = em.createQuery(query.toString(), TblRawMt.class);
            q.setParameter("shortCodes", shortCodes);
            q.setParameter("startDate", startDate);
            q.setParameter("endDate", endDate);
            if (operators != null && !operators.isEmpty()) {
                q.setParameter("operators", operators);
            }
            if (provinces != null && !provinces.isEmpty()) {
                q.setParameter("provinces", provinces);
            }

            return q.getResultList();
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(TblRawMtFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<TblRawMt> find(Date startDate, Date endDate, List<String> shortCodes, List<String> provinces, List<String> operators, String content, String moSyntax) throws Exception {
        try {
            StringBuilder query = new StringBuilder();
            query.append("SELECT t FROM TblRawMt t WHERE t.sc in :shortCodes");
            query.append(" AND t.time BETWEEN :startDate and :endDate");
            if (operators != null && !operators.isEmpty()) {
                query.append(" AND t.telco IN :operators");
            }
            if (provinces != null && !provinces.isEmpty()) {
                query.append(" AND t.gen2 IN :provinces");
            }
            if (content != null && !content.isEmpty()) {
                query.append(" AND t.content LIKE :content");
            }
            if (moSyntax != null && !moSyntax.isEmpty()) {
                query.append(" AND t.gen3 LIKE :moSyntax");
            }
            query.append(" ORDER BY t.sc, t.time DESC, t.content");
            TypedQuery<TblRawMt> q = em.createQuery(query.toString(), TblRawMt.class);
            q.setParameter("shortCodes", shortCodes);
            q.setParameter("startDate", startDate);
            q.setParameter("endDate", endDate);
            if (operators != null && !operators.isEmpty()) {
                q.setParameter("operators", operators);
            }
            if (provinces != null && !provinces.isEmpty()) {
                q.setParameter("provinces", provinces);
            }
            if (content != null && !content.isEmpty()) {
                q.setParameter("content", "%" + content + "%");
            }
            if (moSyntax != null && !moSyntax.isEmpty()) {
                q.setParameter("moSyntax", "%" + moSyntax + "%");
            }
            return q.getResultList();
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(TblRawMtFacade.class.getName()).error("", ex);
            return null;
        }
    }
}
